package com.dc.designPattern._1_singleton.impl.type3;

/**
 * Description: 懒汉式
 * @author: gdc
 * @date: 2020/12/26
 * @version 1.0
 */
class Singleton {

	private static Singleton INSTANCE;

	private Singleton() {}

	//提供一个静态的公有方法，当使用到该方法时，才去创建 INSTANCE
	//即懒汉式
	public static Singleton getINSTANCE() {
		if(INSTANCE == null) {
			INSTANCE = new Singleton();
		}
		return INSTANCE;
	}
}

public class SingletonTest03 {

	public static void main(String[] args) {
		System.out.println("懒汉式1 ， 线程不安全~");
		Singleton instance = Singleton.getINSTANCE();
		Singleton instance2 = Singleton.getINSTANCE();
		System.out.println(instance == instance2); // true
		System.out.println("INSTANCE.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());
	}

}

